import {defineStore} from "pinia";
import {ref} from "vue";

export const useTagsStore = defineStore('tags', () => {
    let headerTags = ref([{path: '/super-ywu/index', meta: { title: '首页' }}])

    function addHeaderTag(routeInfo) {
        let realTagObj = {
            path: routeInfo.path,
            meta: {title: routeInfo.meta.title}
        }
        let listLen = headerTags.value.length
        for (let i = 0; i < listLen; i++) {
            if (headerTags.value[i].path === realTagObj.path) {
                return;
            }
        }
        headerTags.value.push(realTagObj)
    }

    function deleteHeaderRouteTags(tags) {
        let headerValue = headerTags.value
        // 遍历比较path是否相同，相同则删除
        let listLen = headerValue.length
        for (let i = 0; i < listLen; i++) {
            if (headerValue[i].path === tags.path) {
                headerValue.splice(i, 1);
                tags.currentIndex = i
                return;
            }
        }
    }

    return { headerTags, addHeaderTag, deleteHeaderRouteTags }
})